<?php
class Potses extends Controller {
	function Potses(){
		parent::Controller();
    }
    function get($page,$offset,$username=NULL){
    	if($page=='my'){
    		$this->load->model('UserMahasiswa');
			$this->load->model('Posts');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$data ['pageTitle'] = 'Dashboard '.$data['nama'];
			$data['myEcho'] = $this->Posts->getMyPost($this->session->userdata('userId'),$offset);
			if($myEcho = $data['myEcho']){
				$myEchoMsg ="";
				for($i=0;$i<sizeOf($myEcho['message']);$i++){
					$myEchoMsg = $myEchoMsg.$this->_formatEcho($myEcho['user_id'][$i],$myEcho['message'][$i],$myEcho['date'][$i],$myEcho['comments'][$i],$myEcho['post_id'][$i]);	
				}
				echo $myEchoMsg;
			}
    	}elseif($page=='public'){
    		$this->load->model('UserMahasiswa');
			$this->load->model('Posts');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$data ['pageTitle'] = 'Dashboard '.$data['nama'];
			$data['myEcho'] = $this->Posts->getAllPosts($offset,10);
			if($myEcho = $data['myEcho']){
				$myEchoMsg ="";
				for($i=0;$i<sizeOf($myEcho['message']);$i++){
					$myEchoMsg = $myEchoMsg.$this->_formatEcho($myEcho['user_id'][$i],$myEcho['message'][$i],$myEcho['date'][$i],$myEcho['comments'][$i],$myEcho['post_id'][$i]);	
				}
				echo $myEchoMsg;
			}
    	}elseif($page=='friend'){
    	$this->load->model('UserMahasiswa');
			$this->load->model('Posts');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$data ['pageTitle'] = 'Dashboard '.$data['nama'];
			$data['myEcho'] = $this->Posts->getFriendPosts($this->session->userdata('userId'),$offset,10);
			if($myEcho = $data['myEcho']){
				$myEchoMsg ="";
				for($i=0;$i<sizeOf($myEcho['message']);$i++){
					$myEchoMsg = $myEchoMsg.$this->_formatEcho($myEcho['user_id'][$i],$myEcho['message'][$i],$myEcho['date'][$i],$myEcho['comments'][$i],$myEcho['post_id'][$i]);	
				}
				echo $myEchoMsg;
			}
    	}elseif($page=='user'){
    		$this->load->model('User');
			$this->load->model('UsersDetail');
			$this->load->model('UserMahasiswa');
			$this->load->model('Posts');
    		$data['myEcho'] = $this->Posts->getMyPost($this->UsersDetail->getUserIdFromUsername($username),$offset,10);
			$myEchoMsg = "";
    		if($myEcho = $data['myEcho']){
				$myEchoMsg ="";
				for($i=0;$i<sizeOf($myEcho['message']);$i++){
					$myEchoMsg = $myEchoMsg.$this->_formatEcho($myEcho['user_id'][$i],$myEcho['message'][$i],$myEcho['date'][$i],$myEcho['comments'][$i],$myEcho['post_id'][$i]);	
				}
				echo $myEchoMsg;
			}
    	}
    }
    
	function _formatEcho($user_id,$message,$date,$commentNum=NULL,$post_id=NULL){
		
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('User');
		$this->load->model('Posts');
		$this->load->model('Attachment');
		$thisPost = $this->Posts->getPost($post_id);
		
		
		$attachedHTML='<ul id="listAttach">';
		if($thisPost['attached']==1){
			$postAttach = $this->Attachment->getAttachment($post_id);
			for($i=0;$i<sizeOf($postAttach['type']);$i++){
				if($postAttach['type'][$i]=='image'){
					$attachedHTML = $attachedHTML.'<li>
					<a href="'.base_url().'sknimg/i-see-attr/imageAttachment/'.$postAttach['data'][$i].'" class="lightbox-enabled" onclick="$(this).lightbox({start:true,events:false}); return false;" title="'.$postAttach['data'][$i].'">
						<img class="avatarecho" style="float:right;" src="'.base_url().'sknimg/i-see-attr/imageAttachmentThumb/'.$postAttach['data'][$i].'" width="50px" height="50px"  /></a>
					<br><strong style="font-size:11px;font-style:italic;">
					<img src="'.base_url().'sknimg/img/download.png" width="15" height="15"></img>
					<a href="'.base_url().'engine/download/item/'.$postAttach['data'][$i].'/image">
					Image Attachment: '.$postAttach['data'][$i].'</a></strong>
					<div class="tweetTxt">
					';
					$attachedHTML = $attachedHTML.'</div> <div class="clear"></div> </li>';
				}elseif($postAttach['type'][$i]=='file'){
					$attachedHTML = $attachedHTML.'<li>
					<a href="javascript:;"><img class="avatarecho" style="float:right;border:0" src="'.base_url().'sknimg/img/'.'file.png'.'" width="40px" height="45px"  /></a>
					<br><strong style="font-size:11px;font-style:italic;">
					<img src="'.base_url().'sknimg/img/download.png" width="15" height="15"></img>
					<a href="'.base_url().'engine/download/item/'.$postAttach['data'][$i].'/file">
					File Attachment: '.$postAttach['data'][$i].'</a></strong>
					<div class="tweetTxt">
					';
					$attachedHTML = $attachedHTML.'</div> <div class="clear"></div> </li>';
				}
			}
		}
		$attachedHTML=$attachedHTML."</ul>";
		
		$detail = json_decode($this->UsersDetail->getDetail($user_id));
		$avatar = $detail->avatar;
		$username = $this->User->getUsername($user_id);
		$nama = json_decode($this->UserMahasiswa->getNama($user_id))->nama;
		$href = '';
		if($commentNum>5){
			$href = 'href="'.base_url().'post/view/'.$post_id.'"';
		}else{
			$href = 'href="javascript:;" onCLick="getComment('.$post_id.');"';
		}
		//formatting
		$message = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" rel=\"nofollow\">\\2</a>", $message);
		$message = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" rel=\"nofollow\">\\2</a>", $message);
  		$message = preg_replace("/@(\w+)/", "<a href=\"".base_url()."profile/info/"."\\1\" rel=\"nofollow\">@\\1</a>", $message);
  		$message = preg_replace("/#(\w+)/", '<a href="'.base_url().'search/tag/'.'\\1" rel=\"nofollow\">#\\1</a>', $message);
		if($avatar==''){
			$avatar = 'default_ava